package com.lmk.service.impl;

import com.lmk.domain.PurchaseApply;
import com.lmk.domain.PurchaseLog;
import com.lmk.dto.ApplyDto;
import com.lmk.dto.ApprovalDto;
import com.lmk.exception.MyGlobalCustomException;
import com.lmk.mapper.PurchaseApplyMapper;
import com.lmk.mapper.PurchaseLogMapper;
import com.lmk.service.IPurchaseApplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kun
 * @since 2022-09-29
 */
@Service
public class PurchaseApplyServiceImpl extends ServiceImpl<PurchaseApplyMapper, PurchaseApply> implements IPurchaseApplyService {
    @Autowired
    private PurchaseApplyMapper purchaseApplyMapper;
    @Autowired
    private PurchaseLogMapper purchaseLogMapper;

    @Override
    public void apply(ApplyDto applyDto) {
        PurchaseApply purchaseApply = new PurchaseApply();
        purchaseApply.setProductId(applyDto.getProduct_id());
        purchaseApply.setProductName(applyDto.getProduct_name());
        purchaseApply.setStatus(1);// 1代表申请待处理
        purchaseApply.setCount(applyDto.getCount());
        purchaseApply.setCreateTime(new Date());
        purchaseApplyMapper.insert(purchaseApply);
    }

    @Override
    public List<PurchaseApply> applicationquery() {
        return purchaseApplyMapper.selectApply();
    }

    @Override// 2表示审核通过
    public void approval(ApprovalDto approvalDto) {
        PurchaseApply purchaseApply = purchaseApplyMapper.selectById(approvalDto.getId());
        if (approvalDto.getStatus()==2){
            purchaseApply.setStatus(2);
            purchaseApplyMapper.updateById(purchaseApply);
            PurchaseLog log = new PurchaseLog();
            log.setProductId(purchaseApply.getProductId());
            log.setProductName(purchaseApply.getProductName());
            log.setCount(purchaseApply.getCount());
            log.setCreateTime(new Date());
            purchaseLogMapper.insert(log);
        }else {
            throw new MyGlobalCustomException("您的领导给你驳回了");
        }
    }


}
